<template>
  <div class="bg">
    <component
      :is="componentName"
      @Topage="ToSecondPage"
      :data1="supplyMainData"
      :data="data"
      :disabled="disabled"
      @backToMain="back"
    ></component>
  </div>
</template>

<script>
import supplyMain from "../../components/supply/supplyMain.vue"; //发布供应主页面
import plasticMaterial from "../../components/supply/plasticMaterial.vue"; //塑胶原料
import modifiedPlastics from "../../components/supply/modifiedPlastics.vue"; //改性塑料
import reprocessedPlastic from "../../components/supply/reprocessedPlastic.vue"; //再生塑料
import plasticAdditive from "../../components/supply/plasticAdditive.vue"; //塑料助剂
import plasticMachinery from "../../components/supply/plasticMachinery.vue"; //塑料机械

export default {
  components: {
    supplyMain,
    plasticMaterial,
    modifiedPlastics,
    reprocessedPlastic,
    plasticAdditive,
    plasticMachinery,
  },

  data() {
    return {
      componentName: "supplyMain",
      supplyMainData: {},
      data: {},
      disabled: false,
    };
  },

  async created() {
    // console.log("this.$route.name=>",this.$route.name)
    // 已上线，已下线列表跳转
    if (this.$route.params.id) {
      this.disabled = this.$route.params.disabled;
      // console.log("一开始this.disabled", this.disabled);
      this.getDetail(this.$route.params.id);
    }
  },

  watch: {
    "$route.params.id"(newId) {
      if (newId) {
        // console.log("newId",newId);
        this.disabled = this.$route.params.disabled;
        // console.log("监听this.disabled", this.disabled);
        if (this.$route.name == "issue_supply") {
          this.getDetail(newId);
        }
      }
    },
    "$route.name": {
      handler(newName) {
        // console.log("newName", newName);
        if (newName === "issue_supply") {
          this.componentName = "supplyMain";
          
        }
        this.data = {};
        this.supplyMainData = {};
      },
      deep: true,
    },
  },

  methods: {
    // 发布供应新增
    ToSecondPage(data) {
      // console.log("data",data);
      this.supplyMainData = data;

      if (data.typeValue == "塑胶原料") {
        this.componentName = "plasticMaterial";
      } else if (data.typeValue == "改性塑料") {
        this.componentName = "modifiedPlastics";
      } else if (data.typeValue == "再生塑料") {
        this.componentName = "reprocessedPlastic";
      } else if (data.typeValue == "塑料助剂") {
        this.componentName = "plasticAdditive";
      } else if (data.typeValue == "塑料机械") {
        this.componentName = "plasticMachinery";
      }
    },

    // id变化时方法
    async getDetail(id) {
      let params = { id: id };
      let res = await this.$apiFun.productDetail(params);
      // console.log("res",res);
      this.data = res.result;
      if (res.result.type == "PLASTIC") {
        this.componentName = "plasticMaterial";
      } else if (res.result.type == "MODIFIED") {
        this.componentName = "modifiedPlastics";
      } else if (res.result.type == "REGENERATION") {
        this.componentName = "reprocessedPlastic";
      } else if (res.result.type == "ADJUVANT") {
        this.componentName = "plasticAdditive";
      } else if (res.result.type == "MECHANICAL") {
        this.componentName = "plasticMachinery";
      }
    },

    back(data1) {
      // 判断从哪个页面跳转
      if (this.data.status == 1) {
        this.$router.push("/online_products");
      } else if (this.data.status == 0) {
        this.$router.push("/offline_products");
      } else if (data1.typeValue !== "") {
        this.componentName = "supplyMain";
      }

      this.data = {};
      this.supplyMainData = {};
    },
  },
};
</script>

<style lang="less" scoped>
.bg {
  font-family: Microsoft YaHei;
}
</style>